package com.yh.mybatis.session.defaults;

import com.yh.mybatis.executor.Executor;
import com.yh.mybatis.mapping.Environment;
import com.yh.mybatis.session.Configuration;
import com.yh.mybatis.session.SqlSession;
import com.yh.mybatis.session.SqlSessionFactory;
import com.yh.mybatis.session.TransactionIsolationLevel;
import com.yh.mybatis.transaction.Transaction;
import com.yh.mybatis.transaction.TransactionFactory;

/**
 * 默认sql会话工厂
 *
 * @author yh
 * @date 2023/09/25
 */
public class DefaultSqlSessionFactory implements SqlSessionFactory {

    private final Configuration configuration;

    public DefaultSqlSessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public SqlSession openSession() {
        Transaction tx = null;

        final Environment environment = configuration.getEnvironment();
        TransactionFactory transactionFactory = environment.getTransactionFactory();
        tx = transactionFactory.newTransaction(configuration.getEnvironment().getDataSource(), TransactionIsolationLevel.READ_COMMITTED, false);
        //创建执行器
        final Executor executor = configuration.newExecutor(tx);
        //创建DefaultSqlSession
        return new DefaultSqlSession(configuration,executor);
    }
}
